﻿
package com.android.publicModule.model.sms;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsManager;
import android.util.Log;

import java.util.ArrayList;

import com.android.publicModule.tools.Tools;

public class SendSMS {
	
	private static final String LOG_TAG = "platform";//SendSMS.class.getName();

    public SendSMS() {

    }

    public boolean sendSMS(Context context, String sendToNumber, String sendContent) {
    	if(sendToNumber==null||sendToNumber.trim().equals("")||sendContent==null||sendContent.trim().equals(""))
    	{
    		Tools.showLog(LOG_TAG,"sendSMS() 号码或内容为空！sendToNumber="+sendToNumber+" and sendContent"+sendContent);
            return false;
    	}
        SimState simState = new GetCurrentSimState().getCurrentSimState(context);
        if (simState.isSimState())// SIM卡状态良好，发送短信
        {
            Log.e("sendSMS", "sendToNumber=" + sendToNumber + " sendContent=" + sendContent);
            Tools.showLog(LOG_TAG,"sendSMS() sendToNumber=" + sendToNumber + " sendContent=" + sendContent);
            sendSMSDoAction(context, sendToNumber, sendContent);
            return true;
        } else {
        	Tools.showLog(LOG_TAG,"sendSMS() 无SIM卡或SIM卡状态被锁");
            return false;
        }
    }

    private void sendSMSDoAction(Context context, String sendToNumber, String sendContent) {
        SmsManager sms = SmsManager.getDefault();
        PendingIntent sentIntent = PendingIntent.getBroadcast(context, 0, new Intent(), 0);// 发送成功时收到广播
        // 如果字数超过70,需拆分成多条短信发送
        PendingIntent deliveryIntent = null;// 对方接收成功时发送广播
        try {
	        if (sendContent.length() > 70) {
	        	ArrayList<String> msgs = sms.divideMessage(sendContent);
	        	/*
	            for (String msg : msgs) {//分条发送
	                sms.sendTextMessage(sendToNumber, null, msg, sentIntent, deliveryIntent);
	            }
	            */
	            sms.sendMultipartTextMessage (sendToNumber, null, msgs, null, null);//发送长短信
	        } else {
	            sms.sendTextMessage(sendToNumber, null, sendContent, sentIntent, deliveryIntent);
	        }
        } catch(IllegalArgumentException e) {
        	
        } catch(SecurityException e) {
        	Tools.showLog(LOG_TAG,"sendSMSDoAction() 短信发送过程中出现安全性异常，可能被拦截");
        }
    }
}
